#ifndef RENDER_H
#define RENDER_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
#include <QElapsedTimer>
#include <QTimer>
#include <QWheelEvent>
#include "camera.h"
#include <QOpenGLTexture>
#include <QOpenGLFunctions>
#include "model.h"
#include <iostream>
#include <QFile>
#include <QMutex>
#include <QMutexLocker>
#include <QQuickItem>
#include <QQuickFramebufferObject>
#include <QQuickWindow>
#include <QQuickItem>
#include <QOpenGLExtraFunctions>

using namespace std;

struct ModelInfo{
    Model *model;
    QVector3D worldPos;
    float pitch;
    float yaw;
    float roll;
    bool isSelected;
    QString name;
};



class Render : public QObject, protected QOpenGLExtraFunctions
{
    Q_OBJECT
public:
    Render();
    virtual ~Render();

    virtual void initializeGL();

    virtual void resizeGL(int w, int h);
    virtual void render();    //与paintGL相似，但我更喜欢叫render

    void loadModel(string path);
    void setupMesh(const Mesh &mesh, unsigned int &VAO, unsigned int &VBO, unsigned int &EBO);
    Mesh *processMesh(float *vertices, int size, unsigned int textureId);

    QMatrix4x4 projection;

    QFile file;
    unsigned int VBO, VAO;
    // QOpenGLWidget interface
    QMutex line_mutex;
    QMutex model_mutex;
    void init_line();
public slots:
    void goAhead();
    void on_timeOut();
private:
    QTimer m_timer;
    QElapsedTimer m_time;
    QOpenGLShaderProgram m_shaderProgram;
    QOpenGLShaderProgram m_lightShaderProgram;
    QOpenGLShaderProgram line_shaderProgram;
    Camera m_camera;
    QVector3D getCameraInitPos(float min,float max);
    QMap<QString, ModelInfo> m_Models;
    Mesh * m_PlaneMesh;
    Mesh * m_LineMesh;
    // QWidget interface

    QPoint getPoint(QPointF A,QPointF B,QPointF C, float t);


signals:
    void sigUpdate();
};

#endif // RENDER_H
